About: this statistics are compilated with Sidewalks,Crossings and Kerbs data.
All the code is kept here, so anyone can reproduce!
Scroll down and the charts will begin to appear, they we're made with the amazing Altair library, that enables interactivity
Sidewalks Statistics
Crossings Statistics
Kerbs Statistics
currently it's only optimized for desktop
from datetime import datetime
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print('Last Update: ',dt_string)
Last Update: 17/06/2023 13:43:32
import geopandas as gpd
import pandas as pd
import altair as alt
alt.data_transformers.disable_max_rows()
DataTransformerRegistry.enable('default')
def get_count_df(input_df,fieldname,str_to_append=' type'):
outfieldname = fieldname+str_to_append
return input_df[fieldname].value_counts().reset_index().rename(columns={'index':outfieldname,fieldname:'count'}).sort_values(by='count',ascending=False),outfieldname
def create_barchart(input_df,fieldname,title,str_to_append=' type',title_fontsize=24,tooltip='count',x_sort='-y',tooltip_list=['percent']):
# bind = alt.selection_interval(bind='scales')
# .add_selection(bind)
data_to_plot,fieldname_v2 = get_count_df(input_df,fieldname,str_to_append)
feat_count = float(data_to_plot['count'].sum())
def compute_formatted_percent(featureval):
return str(round((featureval/feat_count)*100,2))+"%"
data_to_plot['percent'] = data_to_plot['count'].apply(compute_formatted_percent)
return alt.Chart(data_to_plot,title=title).mark_bar().encode(
x=alt.X(fieldname_v2,sort=x_sort),
y='count',
tooltip=tooltip_list,
).properties(
width=650,
height=300).configure_title(fontSize=title_fontsize).interactive()
def create_barchartV2(input_gdf,fieldname,title,str_to_append=' type',title_fontsize=24,len_field='length(km)'):
# bind = alt.selection_interval(bind='scales')
# .add_selection(bind)
fieldname_v2 = fieldname+str_to_append
data_to_plot = input_gdf[[len_field,fieldname]].groupby([fieldname]).agg({fieldname:'count',len_field:'sum'}).rename(columns={fieldname:'feature count'}).reset_index().rename(columns={fieldname:fieldname_v2})
return alt.Chart(data_to_plot,title=title).mark_bar().encode(
x=alt.X(fieldname_v2,sort='-y'),
y=len_field,
tooltip=len_field,
color='feature count'
).properties(
width=650,
height=300).configure_title(fontSize=title_fontsize).interactive()
def print_relevant_columnames(input_df,not_include=('score','geometry','type','id')):
print(*[f'{column}, ' for column in input_df.columns if not any(word in column for word in not_include)])
def return_weblink(string_id,type='way'):
return f"<a href=https://www.openstreetmap.org/{type}/{string_id}>{string_id}</a>"
def get_year_surveydate(featuredate):
return featuredate.split('-')[0]
sidewalks_gdf = gpd.read_file('../data/sidewalks.geojson')
utm_crs = sidewalks_gdf.estimate_utm_crs()
# sidewalks_data = pd.DataFrame(sidewalks_gdf)
# compute lengths only once:
sidewalks_gdf['length(km)'] = sidewalks_gdf.to_crs(utm_crs).length/1000
sidewalks_gdf['weblink'] = sidewalks_gdf['id'].astype('string').apply(return_weblink)
sidewalks_gdf['Year of Survey'] = sidewalks_gdf['survey:date'].apply(get_year_surveydate)
# sidewalk Length Statistics
sidewalks_gdf['length(km)'].describe()
count 7762.000000 mean 0.036980 std 0.083621 min 0.000553 25% 0.014339 50% 0.022071 75% 0.030271 max 2.855776 Name: length(km), dtype: float64
printing relevant columns on the data:
print_relevant_columnames(sidewalks_gdf)
highway, crossing, traffic_signals, bicycle, mapillary, survey:date, wheelchair, kerb, barrier, access, lit, foot, addr:city, addr:housenumber, addr:street, addr:suburb, name, tactile_paving, surface, smoothness, footway, paving_stones, level, building, covered, lcn, motor_vehicle, segregated, horse, oneway, maxspeed, layer, source, leisure, tunnel, incline, cutting, embankment, dog, cycleway, cycleway:right, ramp, noname, amenity, religion, opening_hours, alt_name, handrail, ramp:wheelchair, step_count, indoor, surface:note, description, lanes, check_date:surface, incline:across, last_update, update_date, length(km), weblink, Year of Survey,
create_barchartV2(sidewalks_gdf,'surface','Sidewalks Surface Type',title_fontsize=24)
create_barchartV2(sidewalks_gdf,'smoothness','Sidewalks Smoothness Level',title_fontsize=24)
create_barchartV2(sidewalks_gdf,'tactile_paving','Sidewalks Tactile Paving Presence',title_fontsize=24)
create_barchartV2(sidewalks_gdf,'width','Sidewalks Width Values',title_fontsize=24)
create_barchartV2(sidewalks_gdf,'incline','Sidewalks Incline Values',title_fontsize=24)
def double_scatter_bar(input_df,title,xs='surface',ys='smoothness',scolor=None,xh='count()',yh1='surface',yh2='smoothness',hcolor=None,fontsize=24,tooltip_fields=['element_type','id']):
interval = alt.selection_interval()
default_color = alt.value('lightseagreen')
if not hcolor:
hcolor = default_color
if not scolor:
scolor = default_color
scatter = alt.Chart(input_df,title=title).mark_point().encode(
x=xs,
y=ys,
color=scolor,
tooltip=alt.Tooltip(tooltip_fields),
).properties(
width=600,
height=350,).add_selection(interval)
hist_base = alt.Chart(input_df).mark_bar().encode(
x=xh,
color=hcolor,
tooltip=alt.Tooltip(tooltip_fields),
).properties(
width=300,
height=220,
).transform_filter(
interval,
)
# if hcolor:
# hist_base.encode(color=hcolor)
hist = hist_base.encode(y=yh1) | hist_base.encode(y=yh2)
return (scatter & hist).configure_title(fontSize=fontsize,align='center')
# 'Surface x Smoothness'
sidewalks_gdf['element_type'].unique()
array(['way'], dtype=object)
double_scatter_bar(sidewalks_gdf,'Surface x Smoothness (sidewalks)',hcolor='length(km)')